
import  requests
import hashlib
import pickle
import re
import time
from server_info import register,login


global_data= {}

#文件路径
unique_number_file_path= 'unique.txt'
#logpath= 'log.log'
test_data_path= 'data.txt'
log_path= 'log.log'

#用例数据变量
total_test_case= 0
success_test_case= 0
fail_test_case= 0

#唯一数据后缀获取
def get_uniquenumber(path):
    with open(path,'r') as fp:
        username_postfix= fp.read().strip()
    with open(path,'w') as fp:
        fp.write(str(int(username_postfix)+1)+'\n')
    return username_postfix
#print(1,get_uniquenumber(file_path))
username= 'kkk'+get_uniquenumber(unique_number_file_path)
#md5加密
def md5(data):
    m5 = hashlib.md5()
    m5.update(data.encode('utf-8'))
    pwd = m5.hexdigest()
    return pwd

#print(1,get_md5('11111'))
#封装发送请求数据的函数
def send_request(url,data,http_method):
    if http_method=='post':
        req= requests.post(url,data)
    return req

#对测试结果进行断言
def assert_result(response_str,assert_word):
    global total_test_case
    global success_test_case
    global fail_test_case
    result= ''
    try:
        total_test_case+=1
        assert re.search(assert_word,response_str)
        success_test_case+=1
        result= '成功'
    except Exception as e:
        fail_test_case+=1
        print('断言失败 %s' %assert_word)
        result= '失败'
        pass
    return result
def write_log(path, url, http_method, data, response_data, elapse_time, assert_result, assert_words):
    with open(path,'a',encoding='utf-8') as fp:
        fp.write('_______'*10+'\n')
        fp.write('测试执行时间:%s\n' %time.strftime('%Y-%m-%d %H:%M:%S'))
        fp.write('接口请求地址：%s\n' %url)
        fp.write('接口请求方法类型：%s\n' %http_method)
        fp.write('接口请求数据:%s\n' %data)
        fp.write('接口响应数据：%s\n' %response_data)
        fp.write('接口响应耗时：%s\n' %elapse_time)
        fp.write('接口断言内容：%s\n' %assert_words)
        fp.write('接口断言结果：%s\n' %assert_result)
        fp.write('_______'*10+'\n')
def output_result(total,success,failed):
    print('--'*50)
    print('测试结果汇总：')
    print('一共执行测试用例%s个' %total)
    print('成功执行测试用例%s个' %success)
    print('失败执行测试用例%s个' %failed)


#读取每行数据
def get_testcase_data(path):
    with open(path,'r') as fp:
        data= fp.readlines()
        data=[line.strip() for line in data]
    return data

#每行数据处理
def pre_handle_data(line):
    if re.search(r'\$\{(\w+)\}',line):
        var_name= re.search(r'\$\{(\w+)\}', line).group(1)
        var_value=eval(var_name)
        line= re.sub(r'\$\{(\w+)\}',var_value,line)
        #print(0,line)
    if re.search(r"\$\{md5\('\w+'\)\}", line):
        #print(2,line)
        md5_data = re.search(r"\$\{(md5\('\w+'\))\}",line).group(1)
        #print(1,md5_data)
        md5_data=eval(md5_data)
        #print(1,md5_data)
        line= re.sub(r"\$\{(md5\('\w+'\))\}",md5_data,line)
        print(3, line)
    #print(1,line)
    #pass
    return line


#响应数据处理
def post_handle_data(correlation_exp,resp):
    global global_data
    var_name= correlation_exp.split('----')[0]
    reg_exp= correlation_exp.split('----')[1]
    if re.search(reg_exp,resp):
        value= re.search(reg_exp,resp).group(1)
        global_data[var_name]= value